<?php


/*
 * 支付		会员模型
 * Author	1041865683@qq.com
 * Date		2018.4.14
 */


/* 
 * 原生 - 创建用户
 * @param  $arr	要创建的信息
 * @return $r	返回是否成功 Boolean		
 */
function user__create($arr) {
	$r = db_insert('user', $arr);
	return $r;
}


/*
 * 原生 - 用户更新
 * @param	$uid	会员ID
 * @param	$update	要更改的信息数组
 * @return	$r		是否成功 Boolean
 */
function user__update($uid, $update) {
	$r = db_update('user', array('uid'=>$uid), $update);
	return $r;
}


/*
 * 原生 - 读取用户数据
 * @param	$uid	会员ID
 * @return  $user	用户信息 Array
 */
function user__read($uid) {
	$user = db_find_one('user', array('uid'=>$uid));
	return $user;
}


/*
 * 原生 - 删除用户
 * @param	$uid	会员ID
 * @return	$r		是否成功 Boolean
 */
function user__delete($uid) {
	$r = db_delete('user', array('uid'=>$uid));
	return $r;
}


/*
 * 会员注册
 * @param	$arr	注册信息数组 username password mobile
 * @return	$r		是否成功 Array
 */
function user_register($arr){
	global $time,$ip,$conf,$db;
	$username = $arr['username'];
	$password = $arr['password'];
	$email = $arr['email'];
	$ulen = strlen($username);
	$plen = strlen($password);
	if($ulen>15 || $ulen<5) return array(0,lang('username_length_is_incorrect'));
	if($plen>16 || $plen<6) return array(0,lang('password_length_is_incorrect'));
	if(!preg_match('/\@/',$email)) return array(0, '邮箱格式错误');
	if(user_read_by_username($username)) return array(0,lang('username_is_in_use'));
	$salt = rand(1000,9999);
	$is_open = 0;
	$array = array(
		'email' => $email,
		'username' => $username,
		'password' => md5(md5($password).$salt),
		'password_ti' => md5(md5($password).$salt),
		'salt' => $salt,
		'parent' => isset($arr['parent']) ? $arr['parent'] : 0,
		'rate' => isset($arr['rate']) ? $arr['rate'] : 0,
		'is_open' => $is_open,
		'create_time'=>$time,
		'login_time'=>$time,
		'create_ip'=>$ip,
		'login_ip'=>$ip
	);
	$res = user__create($array);
	if($res){
		$user = user_read_by_username($username);
		user__update($user['uid'], array('apino'=>user_apino($user['uid']),'apisecret'=>user_apisecret($user['uid'], $user['username'], $user['email'])));	// 密匙
        db_sql_find_one('SET AUTOCOMMIT=0');
//        try{
            $cate = db_sql_find("SELECT * FROM ".$db->tablepre."cate where is_public=1 and status=1");//所有的公共店铺
            foreach ($cate as $key=>$value){
                $arr = array('uid'=>$user['uid'],'name'=>$value['name'],'is_public'=>2,'status'=>1);
                $cate_id = db_insert('cate',$arr);//新用户的复制店铺id
                $price = db_sql_find("SELECT * FROM ".$db->tablepre."cate_price where cid=".$value['id']." and status=1");//查询所有的价格段
                foreach ($price as $k=>$v){
                    $arr = array('cid'=>$cate_id,'price_start'=>$v['price_start'],'price_end'=>$v['price_end'],'status'=>1);
                    $price_id = db_insert('cate_price',$arr);//新用户的复制价格段id
                    $goods = db_sql_find("SELECT * FROM ".$db->tablepre."cate_goods where cid=".$value['id']." and pid=".$v['id']." and status=1");
                    foreach ($goods as $ke=>$va){
                        $arr = array('cid'=>$cate_id,'pid'=>$price_id,'name'=>$va['name'],'status'=>1);
                        db_insert('cate_goods',$arr);
                    }
                }
            }
//        }catch (Exception $e){
//            return array(1,'注册成功！请耐心等待审核通过。添加默认模板失效');
//        }
        db_sql_find_one('COMMIT');
		return array(1,'注册成功！请耐心等待审核通过');
	}else{
		return array(0,lang('user_create_failed'));
	}
}


/*
 * 会员登陆
 * @param	$arr	会员信息数组 username password
 * @return  $r		是否登陆成功 Array
 */
function user_login($arr){
	global $time,$ip,$db,$conf;
	$username = $arr['username'];
	$password = $arr['password'];
	$ulen = strlen($username);
	$plen = strlen($password);
	if($ulen>20 || $ulen<5) return array(0,lang('username_length_is_incorrect'));
	if($plen>20 || $plen<6) return array(0,lang('password_length_is_incorrect'));
	$user = db_find_one('user', array('username'=>$username), array(), array('salt,uid,password,is_open,allow_ips'));
	//白名单检测
	if($user['allow_ips']!='' && strpos($user['allow_ips'],$ip)===false){//不在白名单内
		return array(0,'IP不在白名单内');
	}
	//
	if(!$user) return array(0,lang('user_not_exists'));
	if($user['password']!=md5(md5($password).$user['salt'])) return array(0,lang('password_incorrect'));
	if($user['is_open']==0) return array(0, '您的帐号正在审核中...');
	$array = array(
		'login_time'=>$time,
		'login_ip'=>$ip
	);
	user__update($user['uid'],$array);
	user_token_set($user['uid']);
	user_log_insert(array('uid'=>$user['uid'], 'type'=>'登录', 'detail'=>$ip, 'time'=>$time));
	return array(1,lang('login_successfully'));
}


/*
 * 更改密码
 * @param	$arr	新旧密码数组 new old
 * @return  $r		是否更改成功 Boolean
 */
function user_password_change($arr){
	global $user;
	$new = $arr['new'];
	$old = $arr['old'];
	$old_len = strlen($old);
	$new_len = strlen($new);
	if($old==$new) return array(0,'新密码不能和原密码一致');
	if($old_len>15 || $old_len<6 || $new_len>15 || $new_len<6) return array(0,lang('password_length_is_incorrect'));
	//
	if(isset($arr['type']) && $arr['type']==1){//修改提款密码
		if($user['password_ti']!=md5(md5($old).$user['salt'])) return array(0,'原提款密码错误');
		$password = md5(md5($new).$user['salt']);
		user__update($user['uid'], array('password_ti'=>$password));
		return array(1,'修改提款密码成功！');	
	}else{//修改登陆密码
		if($user['password']!=md5(md5($old).$user['salt'])) return array(0,'原登陆密码错误');
		$password = md5(md5($new).$user['salt']);
		user__update($user['uid'], array('password'=>$password));
		return array(1,'修改登陆密码成功！请重新登录');
	}
}


/*
 * 通过UID读取会员信息
 * @param	$uid	会员ID
 * @rerurn	$user	会员信息数组
 */
function user_read($uid){
	$user = user__read($uid);
	user_format($user);
	return $user;
}


/*
 * 通过用户名读取会员信息
 * @param	$username	会员用户名
 * @rerurn	$user	会员信息数组
 */
function user_read_by_username($username){
	$user = db_find_one('user', array('username'=>$username));
	user_format($user);
	return $user;
}


/*
 * 通过微信open_id读取会员信息
 * 微信登陆
 * @param	$id		会员的微信open_id
 * @rerurn	$user	会员信息数组
 */
function user_read_by_openid($id){
	$user = db_find_one('user', array('wx_openid'=>$id));
	user_format($user);
	return $user;
}


/*
 * 通过APINO获取会员信息
 * @param apino 商户ID
 * @return array 会员信息
 */
function user_read_by_apino($apino){
	$user = db_find_one('user', array('apino'=>$apino));
	user_format($user);
	return $user;
}


/*
 * 会员信息格式化
 * @param	$user	会员信息数组
 * @rerurn	$user	会员信息数组
 	* @param	groupname	用户组名称
	* @param	address		会员收货地址数组
 */
function user_format(&$user){
	if($user){
		
	}
}


/*
 * 获取会员用户组
 */
function user_group($uid){
	$user = db_find_one('user', array('uid'=>$uid), array(), array('gid'));
	return $user['gid'];
}


/*
 * 获取会员登陆COOKIE
 * @return	$_uid	会员的COOKIE的UID
 */
function user_token_get() {
	global $time;
	$_uid = user_token_get_do();
	if(!$_uid) setcookie('login_token', '', $time - 864000, '');
	return $_uid;
}


/*
 * 破解会员登陆COOKIE
 * @return	$_uid	会员的COOKIE的UID
 */
function user_token_get_do() {
	global $time, $ip, $conf;
	$token = param('login_token');
	if(empty($token)) return FALSE;
	$tokenkey = md5($conf['auth_key']);
	$s = xn_decrypt($token, $tokenkey);
	if(empty($s)) return FALSE;
	$arr = explode("\t", $s);
	if(count($arr) != 3) return FALSE;
	list($_ip, $_time, $_uid) = $arr;
	return $_uid;	
}


/*
 * 添加会员登陆COOKIE
 * @param	$_uid	会员的COOKIE的UID
 */
function user_token_set($uid) {
	global $time, $conf;
	if(empty($uid)) return;
	$token = user_token_gen($uid);
	setcookie('login_token', $token, $time + 864000, $conf['cookie_path']);
}


/*
 * 删除会员登陆COOKIE
 */
function user_token_clear() {
	global $time, $conf;
	setcookie('login_token', '', $time - 864000, $conf['cookie_path']);
	setcookie('wx_openid', '', $time - 864000, $conf['cookie_path']);	//微信登陆授权
	setcookie('_user_login', '', $time - 864000, $conf['cookie_path']);	//论坛登陆授权
	setcookie('ihx__Session', '', $time - 864000, $conf['cookie_path']);
	setcookie('ihx__user_login', '', $time - 864000, $conf['cookie_path']);
}


/*
 * 加密会员登陆COOKIE UID
 * @param	$uid	要加密的会员UID
 * @param	$token	加密后的会员UID
 */
function user_token_gen($uid) {
	global $ip, $time, $conf;
	$tokenkey = md5($conf['auth_key']);
	$token = xn_encrypt("$ip	$time	$uid", $tokenkey);
	return $token;
}


/*
 * 添加会员操作记录
 * @param arr 操作记录数组
 * @return boolean 操作是否成功
 */
function user_log_insert($arr){
	$r = db_insert('user_log', $arr);
	return $r;
}


/*
 * 读取会员操作记录
 * @param cond 查询条件
 * @param page 第几页
 * @param pagesize 每页显示几条
 * @return array 查询结果
 */
function user_log_find($cond, $page=1, $pagesize=10){
	$r = db_find('user_log', $cond, array('time'=>'-1'), $page, $pagesize);
	return $r;
}


/*
 * 生成接入编号
 * @param uid 会员ID
 * @return String 编号
 */
function user_apino($uid){
	$apino = 10000 + $uid;
	return $apino;
}


/*
 * 生成接入密匙
 * @param uid 会员ID
 * @param username 用户名
 * @param email 邮箱
 * @return String 密匙
 */
function user_apisecret($uid, $username, $email){
	$secret = md5(md5(md5($username).$uid).$email);
	return $secret;
}


?>